# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
_luaversions="5.1 5.2 5.3"
pkgname=lua-evdev
_rockname=evdev
pkgver=2.2.1
pkgrel=1
pkgdesc="Lua module for reading input events"
url="https://github.com/Tangent128/lua-evdev"
arch="all"
license="MIT"
depends=""
makedepends="linux-headers"
subpackages=""
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_package"
done
source="$pkgname-$pkgver.tar.gz::https://github.com/Tangent128/$pkgname/archive/evdev-$pkgver.tar.gz
	time64.patch"
builddir="$srcdir/$pkgname-evdev-$pkgver"

prepare() {
	default_prepare || return 1

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver" || return 1
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		cd "$builddir-$lver"
		make MYCFLAGS="$CFLAGS $(pkg-config --cflags lua$lver)" || return 1
	done
}

package() {
	mkdir -p "$pkgdir"
}

_package() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="$depends lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_rockname/$pkgver-1"

	cd "$builddir-$lver"

	local f; for f in evdev.lua evdev/constants.lua; do
		install -D $f "$subpkgdir"/usr/share/lua/$lver/$f || return 1
	done
	install -D evdev/core.so "$subpkgdir"/usr/lib/lua/$lver/evdev/core.so

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="6d8bfb148013c780f7276c884e297b7d6ea55c0c257ac074c677b0dc6495a6087e0e1cb671d844b22d13c9a4c19c544d22a10f3ded5ad3580ccce33c74690871  lua-evdev-2.2.1.tar.gz
6842d2fb223aa44ae799b0f2841d4847c3bbe83d64dc643cf843592a4e84bc0f734b745868985b3cb1977417c9afce2108b5a8c4e8c8d147529b9c4bee1b473e  time64.patch"
